from django.db import models
from django.contrib.auth.models import User
from utils import choices
import os


def attachment_upload(instance,filename):
    id = "account_" + str(instance.pk)
    (shotname, extension) = os.path.splitext(filename);
    path = os.path.join('images', id, 'avatar'+ extension)
    return path

def attachment_upload_thumbs(instance,filename):
    id = "account_" + str(instance.pk)
    (shotname, extension) = os.path.splitext(filename);
    path = os.path.join('images', id, 'avatar_thumbs'+ extension)
    return path


class Account(models.Model):
    # 使用 django 默认 User 表作为验证基础
    # User 和 Account 为一对一关系
    # 也可采用继承 AbstractUser 的方式
    user = models.OneToOneField(
        User,
        unique=True,
        verbose_name='对应验证账号',
        on_delete=models.PROTECT,
    )
    # 真实信息
    real_name = models.CharField(max_length=20, verbose_name='真实姓名', blank=True)
    # 网站用户信息
    nickname = models.CharField(verbose_name='昵称', max_length=50, blank=True, default='  ')
    # Email
    email = models.EmailField('用户邮箱地址', unique=True, null=True, blank=True)
    role = models.CharField(
        verbose_name="角色",
        max_length=10,
        choices=choices.ACCOUNT_ROLE_CHOICES,
    )
    member_id=models.IntegerField(
        default=-1,
#        unique=True,
    )
    gender = models.CharField(
        verbose_name='性别',
        max_length=10,
        choices=choices.GENDER_CHOICES,
        default='init',
        blank=True,
    )

    # Phone
    phone = models.CharField(verbose_name='手机号', max_length=11, unique=True)
    phone_confirmation_time = models.DateTimeField(
        '短信验证的时间(注册时间)', blank=True, null=True
    )

    college = models.CharField(
        verbose_name="大学",
        blank=True,
        default='1',
        max_length=20,
        null=True,
    )
    # temporally comment
    avatar = models.ImageField(
        verbose_name='头像',
        upload_to=attachment_upload,
        blank=True,
        null=True)

    avatar_thumb = models.ImageField(
        verbose_name='头像缩略',
        upload_to=attachment_upload_thumbs,
        blank=True,
        null=True,
       # sizes=((150, 150),)
    )




class StudentInfo(models.Model):
    last_login_time = models.DateTimeField(auto_now_add=True, )
    Join_date = models.DateField(
        null=True,
        blank=True,
    )
    grade = models.CharField(
        max_length=60,
        null=True,
        blank=True,
    )
    class_id = models.IntegerField(
        null=True,
        blank=True,
    )
    student_number = models.CharField(
        max_length=40,
        null=True,
        blank=True,
    )
    major = models.CharField(
        max_length=60,
        null=True,
        blank=True,
    )
    institute_id = models.IntegerField(
        null=True,
        blank=True,
    )


class TeacherInfo(models.Model):
    last_login_time = models.DateTimeField(auto_now_add=True, )
    Join_date = models.DateField(
        null=True,
        blank=True,
    )
    position = models.CharField(
        max_length=40,
        null=True,
        blank=True,
    )
    job_number = models.IntegerField(
        null=True,
        blank=True,
    )


class EmailConfirmation(models.Model):
    account_id = models.IntegerField()
    activation_key = models.CharField(
        max_length=20,
        blank=True,
        null=True,
    )
    send_time = models.DateTimeField(auto_now_add=True, )
    confirmation_time = models.DateTimeField(
        blank=True,
        null=True,
    )


class College(models.Model):
    name = models.CharField(
        verbose_name="学校",
        max_length=60,
        unique=True,
    )
    def __str__(self):
        return self.name


class Class(models.Model):
    college_id=models.IntegerField(
        verbose_name='学校id',
    )

    institue_id=models.IntegerField(
        verbose_name='学院id'
    )

    class_name=models.CharField(
        verbose_name='班级名称',
        max_length=30,
    )


class Institue(models.Model):
    college_id = models.ForeignKey(College, verbose_name="学校ID", on_delete=models.CASCADE)
    name = models.CharField(
        verbose_name="学院名",
        max_length=60,
        unique=True,
    )
    def __str__(self):
        return self.name

class VerifyRecord(models.Model):
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    status = models.CharField(
        verbose_name='状态',
        choices=choices.VERIFY_RECORD_STATUS_CHOICES,
        default='0',
        max_length=10
    )
    code = models.CharField(
        'Code',
        max_length=10,
    )
    is_for=models.CharField(
        verbose_name="验证码用途",
        choices=choices.SMS_USSAGE,
        max_length=2,
        default="1",
    )
    class Meta:
        abstract = True


class SmsRecord(VerifyRecord):
    phone = models.CharField(
        verbose_name='手机号',
        max_length=11,
    )

    class Meta:
        verbose_name = verbose_name_plural = '短信验证记录'
        ordering = ['-create_time']

    def __str__(self):
        return str(self.phone) + ' : ' + str(self.create_time)
